package ExerciseOne;

public class SingletonTest {

	public static void main(String[] args) {
//		Bank bank1 = Bank.getInstance();
//		Bank bank2 = Bank.getInstance();
//		System.out.println(bank1 == bank2);//true
//		Oder oder1 = Oder.getOder();
//		Oder oder2 = Oder.getOder();
//		System.out.println(oder1 == oder2);

//		Per p1 = Per.getP();
//		Per p2 = Per.getP();
//		System.out.println(p1 == p2);
		A a1 = A.getA();
		A a2 = A.getA();
		System.out.println(a1 == a2);
	}

}

//饿汉式
class Bank
{
	//私有化构造器
	private Bank()
	{
		
	}
	
	//内部创建类的对象
	private static Bank instance = new Bank();
	
	//返回类的对象
	public static Bank getInstance()
	{
		return instance;
	}
}

//饿汉式
class Oder
{
	private Oder()
	{
		
	}
	
	private static Oder oder = new Oder();
	
	public static Oder getOder()
	{
		return oder;
	}
}

//懒汉式
class Per
{
	//私有化构造器
	private Per()
	{
		
	}
	
	private static Per P = null;
	
	public static Per getP()
	{
		if(P == null)
		{
			P = new Per();
		}
		return P;
	}
}

class A
{
	private A()
	{
		
	}
	
	private static A a = null;
	
	public static A getA()
	{
		if(a == null)
		{
			a = new A();
		}
		return a;
	}
}